import { StringConstant } from './Constant'

export class contentRegExp {
  /**
   * 根据正则表达式截取@CompileArkTS编译时间，返回匹配的结果
   *
   * @param fileContent 文件内容
   * @returns
   */
  static compileArkTSTime(fileContent: string): string {
    const arkTsRe: RegExp = new RegExp(StringConstant.ARKTS_REG, "g");
    const ArkTsList: RegExpMatchArray | null = fileContent.match(arkTsRe);

    let time: string = "";
    if (ArkTsList) {
      time = ArkTsList[0];
    }

    return time;
  }

  /**
   * 根据正则表达式截取SUCCESSFUL in后的时间，返回匹配的结果
   *
   * @param fileContent 文件内容
   * @returns
   */
  static successfulTime(fileContent: string) {
    const succRe: RegExp = new RegExp(StringConstant.SUCCESSFUL_REG, "g");
    const succList: RegExpMatchArray | null = fileContent.match(succRe);

    let time: string = "";
    if (succList) {
      time = succList[succList.length - 1];
    }

    return time;
  }

  /**
   * 根据正则表达式截取秒数并返回
   * 
   * @param time 
   * @returns 
   */
  static secondNum(time: string): number {
    const secRe: RegExp = new RegExp(StringConstant.SECOND_NUM_REG, "g");
    const secondList: RegExpMatchArray | null = time.match(secRe);
    let seconds: number = 0;
    if (secondList) {
      seconds = Number(secondList[0]);
    }

    return seconds;
  }

  /**
   * 根据正则表达式截取毫秒数并返回
   * 
   * @param time 
   * @returns 
   */
  static millisecondNum(time: string): number {
    const millisecRe: RegExp = new RegExp(StringConstant.MILLISECOND_NUM_REG, "g");
    const millisecList: RegExpMatchArray | null = time.match(millisecRe);
    let milliseconds: number = 0;
    if (millisecList) {
      milliseconds = Number(millisecList[0]);
    }

    return milliseconds;
  }

  /**
   * 返回告警内容
   * 
   * @param file 
   * @returns 
   */
  static alarmContent(file: string): string[] {
    let alarmList: string[] = []
    const content: string[] = file.split(StringConstant.HVIGOR_REG)
    let alarmIndex = 0
    content.forEach((item, index) => {
      if(item.indexOf(StringConstant.COMPILEARKTS_REG) !== -1) {
        alarmIndex = index
      }
    })

    if(alarmIndex !== 0){
      alarmList = alarmList.concat(contentRegExp.getAlarmInfo(content[alarmIndex-1]))
      alarmList = alarmList.concat(contentRegExp.getAlarmInfo(content[alarmIndex+1]))
    }
    return alarmList
  }

  /**
   * 获取ArkTS:WARN、ArkTS:ERROR与ERROR告警内容
   * 
   * @param content 
   * @returns 
   */
  static getAlarmInfo(content: string): string[] {
    let alarmArr: string[] = []
    const warnRe: RegExp = new RegExp(StringConstant.ARKTS_WARN, 'gs');
    const errorRe: RegExp = new RegExp(StringConstant.ARKTS_ERROR, 'gs');
    const failedErrorRe: RegExp = new RegExp(StringConstant.FAILED_ERROR, 'g');
    const warnContent: RegExpMatchArray | null = content.match(warnRe)
    const errorContent: RegExpMatchArray | null = content.match(errorRe)
    const failedErrorContent: RegExpMatchArray | null = content.match(failedErrorRe)

    if(warnContent) {
      alarmArr = alarmArr.concat(warnContent) 
    }
    if(errorContent) {
      alarmArr = alarmArr.concat(errorContent)
    }
    if(failedErrorContent) {
      alarmArr = alarmArr.concat(failedErrorContent)
    }
    return alarmArr;
  }
}
